<template>
  <div class="navigation-wrapper">
    <ul class="module-navigation">
      <li v-for="(item,index) in items" @click="current = index">
        <v-module :module="item"></v-module>
      </li>
    </ul>
    <router-view style="width:160px;"></router-view>
  </div>
</template>
<script>
import module from './module'
export default {
  name: 'navigation',
  props: {
    modules: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      current: undefined,
      defaultProps: {
        label: 'name'
      }
    }
  },
  computed: {
    items() {
      return this.modules.map((item) => {
        this.$set(item, 'active', false)
        return item
      })
    }
  },
  watch: {
    current: function (newIndex, oldIndex) {
      this.items[oldIndex === undefined ? newIndex : oldIndex].active = false
      this.items[newIndex].active = true

      this.jump()
    }
  },
  updated() {
    if (this.current !== undefined) {
      this.jump()
    }
  },
  methods: {
    jump() {
      this.$router.push({path: '/' + this.items[this.current].code, query: {list: this.items[this.current].menus}})
    }
  },
  components: {
    'v-module': module
  }
}
</script>
<style lang='less'>
@import '../assets/css/base.less';
.navigation-wrapper {
  display: flex;
  background: #e9e9e9;
  .module-navigation {
    display: flex;
    flex-direction: column;

    width: 70px;

    background-color: @theme-color;
    li:first-child {
      border-top: 1px #434143 solid;
    }
    li {
      cursor: pointer;
      border-bottom: 1px #434143 solid;
    }
  }
}
</style>
